from flask_wtf.file import FileField, FileRequired, FileAllowed

from app.libs.tools import parseDoc
from app.validators.base import BaseForm as Form
from app.validators.filebase import FileBaseForm
from wtforms import StringField, ValidationError, IntegerField


class DocUploadForm(FileBaseForm):
    file = FileField('file', validators=[FileRequired(message='请选择文件上传'),
                                         FileAllowed(['docx', 'doc'], message='仅允许上传docx,doc格式文件！'),
                                         ])

    def __init__(self, g):
        super(DocUploadForm, self).__init__()
        self.uid = g.user.uid


class PersonalDocumentDeleteForm(Form):
    id = IntegerField()


class PersonalCheckUploadSubmitForm(Form):
    title = StringField()
    file_path = StringField()
    content = StringField()

    def validate_file_path(self, field):
        try:
            self.content.data = parseDoc(field.data)
        except Exception as e:
            print(e)
            raise ValidationError('文档读取失败！')


class PersonalCheckPasteSubmitForm(Form):
    title = StringField()
    content = StringField()
